using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ASP_TF2LogParser.Admin
{
    public partial class UploadLog : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {

            LogParser.DA.DataObjects.LeaguesDS.LeagueSelectDataTable dt = LogParser.DA.LeagueModel.GetLeagueDataTable();

            foreach ( LogParser.DA.DataObjects.LeaguesDS.LeagueSelectRow dr in dt.Rows )
            {
                ListItem li = new ListItem();
                li.Text = dr.LeagueName;
                li.Value = dr.LeagueId.ToString();

                LeagueNumberDropdown.Items.Add( li );
            }

        }

        protected void btnUpload_Click( object sender, EventArgs e )
        {


            if ( FileUpload1.HasFile )
            {
                int leagueId = Convert.ToInt32( LeagueNumberDropdown.SelectedItem.Value );

                FileUpload1.SaveAs( Server.MapPath( "~/Admin/LogFiles/" + FileUpload1.FileName ) );
                LogParser.Parser uploadParser = new LogParser.Parser( Server.MapPath( "~/Admin/LogFiles/" + FileUpload1.FileName ), leagueId );
                uploadParser.Parse();

                LogParser.DataPopulater dp = new LogParser.DataPopulater( uploadParser );
                bool result = dp.PersistDataToDatabase();

                if ( result )
                {
                    lblResult.Text = "Parse completed successfully!";
                }
                else
                {
                    lblResult.Text = "An error occurred while parsing the file.";
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged( object sender, EventArgs e )
        {

        }
    }
}
